package com.example.neusoft215gdh.androidpro_21.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.neusoft215gdh.androidpro_21.Common.utilMeth;
import com.example.neusoft215gdh.androidpro_21.JavaBean.StringBean;
import com.example.neusoft215gdh.androidpro_21.Listener.UserListener;
import com.example.neusoft215gdh.androidpro_21.Model.UserModel;
import com.example.neusoft215gdh.androidpro_21.R;

import static com.example.neusoft215gdh.androidpro_21.R.id.login_cb;
import static com.example.neusoft215gdh.androidpro_21.R.id.user;

public class LoginActivity extends AppCompatActivity  implements utilMeth,View.OnClickListener,UserListener {

    private Button login;
    private TextView regUser;
    private Button loginUser;
    public EditText username;
    public EditText password;
    public CheckBox rember;
    SharedPreferences sp;
    SharedPreferences.Editor editor;
    String uname;
    String pwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initViews();
        login.setOnClickListener(this);

        /**
         *判断是否点击了记住我
         */
        sp = getSharedPreferences("remember", MODE_PRIVATE);
        if (sp.getBoolean("status", false)) {
            rember.setChecked(true);
            username.setText(sp.getString("USER_NAME", ""));
            password.setText(sp.getString("PASSWORD", ""));
        } else {
            rember.setChecked(false);
        }

        inEvents();
    }

    public void initViews() {
        regUser = (TextView) findViewById(R.id.regUser);
        loginUser = (Button) findViewById(R.id.login);
        username = (EditText) findViewById(user);
        password = (EditText) findViewById(R.id.password);
        login= (Button) findViewById(R.id.login);
        rember= (CheckBox) findViewById(login_cb);

    }

    public void inEvents() {

        //触发记住我功能
        rember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                editor = sp.edit();
                if (isChecked) {
                    editor.putBoolean("status", true);
                    editor.putString("USER_NAME", username.getText().toString());
                    editor.putString("PASSWORD", password.getText().toString());
                } else {
                    editor.putBoolean("status", false);
                }
                editor.commit();
            }
        });


        regUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login:
                uname=username.getText().toString();
                pwd=password.getText().toString();
                UserModel userModel=new UserModel();
                userModel.getLogin(uname,pwd,this);
                break;
        }
    }

    @Override
    public void onResponse(StringBean stringBean) {

        Toast.makeText(this, "登录成功!", Toast.LENGTH_SHORT).show();
        savaUser(stringBean.getId(),stringBean.getSessionid());
        Intent intent=new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(this, ""+msg, Toast.LENGTH_SHORT).show();
    }

    public  void savaUser(String userid,String sessionid){

        SharedPreferences sharedPreferences=getSharedPreferences("Userid", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit=sharedPreferences.edit();
        edit.putString("user_id",userid);
        edit.putString("sessionId",sessionid);
        edit.commit();
    }

}
